{ make color table
  only pascal-written data for MULE.
  calculates color tables. feel free to port this to C if you
  want to. i saw no need for that... }

uses dos,crt,spicture,picform;

var
  colortable      : array[0..63]of ^scrntype;
  r,g,b,rb,gb,bb  : integer;
  nc,ni,i         : integer;

  o,f             : word;

begin
  asm mov ax,$13; int $10; end;
  getmem (ruutu,64000);
  screen := ptr (segA000,0);
  temp_segment := seg(ruutu^);
  unpack_lbm ('gfx\earth2.lbm',$a000,gen_palette);
  setpale (100); videoon;
  movemem (screen^,ruutu^,32000);
  for zz := 0 to 15 do
    begin
      directvideo := false;
      gotoxy (1,1); write (zz:2);
      getmem (colortable[zz],256);

      for yy := 0 to 255 do
        begin
          rb := integer(gen_palette[yy*3+0]) * zz div 15;
          gb := integer(gen_palette[yy*3+1]) * zz div 15;
          bb := integer(gen_palette[yy*3+2]) * zz div 15;
          if rb > 63 then rb := 63;
          if gb > 63 then gb := 63;
          if bb > 63 then bb := 63;

          nc := 0; ni := 32500;

            for xx := 0 to 255 do
            begin
              r := rb - gen_palette[xx*3+0];
              g := gb - gen_palette[xx*3+1];
              b := bb - gen_palette[xx*3+2];
              i := (r*r+g*g+b*b);
              if i < ni then
                begin
                  nc := xx; ni := i;
                end;
            end;
          colortable[zz]^[yy] := nc;
        end;
    end;
  for zz := 0 to 15 do
    begin
      directvideo := false;
      gotoxy (1,1); write (zz+16:2);
      getmem (colortable[zz+16],256);
      for yy := 0 to 255 do
        begin
          rb := 63+integer(gen_palette[yy*3+0]-63) * (15-zz) div 15;
          gb := 63+integer(gen_palette[yy*3+1]-63) * (15-zz) div 15;
          bb := 63+integer(gen_palette[yy*3+2]-63) * (15-zz) div 15;
          if rb > 63 then rb := 63;
          if gb > 63 then gb := 63;
          if bb > 63 then bb := 63;

          nc := 0; ni := 32500;
          for xx := f to 255 do
            begin
              r := rb - gen_palette[xx*3+0];
              g := gb - gen_palette[xx*3+1];
              b := bb - gen_palette[xx*3+2];
              i := (r*r+g*g+b*b);
              if i < ni then
                begin
                  nc := xx; ni := i;
                end;
            end;
          colortable[zz+16]^[yy] := nc;
        end;
    end;

  assign (tied,'color.dat'); rewrite (tied,1);
  for zz := 0 to 31 do
    begin
      blockwrite (tied,colortable[zz]^,256);
      for o := 0 to 63999 do
        begin
          screen^[o] := colortable[zz]^[ruutu^[o]];
        end;
      delay (50);
    end;
  close (tied);
end.